package com.nanjia.boot.myflow.order.dto;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import cn.idev.excel.annotation.ExcelIgnore;
import cn.idev.excel.annotation.ExcelIgnoreUnannotated;
import cn.idev.excel.annotation.ExcelProperty;
import cn.idev.excel.annotation.format.DateTimeFormat;
import cn.idev.excel.annotation.write.style.ColumnWidth;
import cn.idev.excel.annotation.write.style.ContentStyle;
import cn.idev.excel.enums.poi.HorizontalAlignmentEnum;
import cn.idev.excel.enums.poi.VerticalAlignmentEnum;
import com.nanjia.boot.common.base.IBaseEnum;
import com.nanjia.boot.common.constant.DataConstatns;
import com.nanjia.boot.common.enums.OrderStatusEnum;
import com.nanjia.boot.common.excelhandler.ExcelMergeColumn;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 这个地方，因为合并会多次写cell，是非常耗时的，建议在生成数据时提前将被合并的那一行的数据设为空，减少IO，数据量小的时候无所谓
 *
 * @author 周洋
 * @since Created in 2025 2025/8/27 17:24
 */
@Data
@ColumnWidth(20)
@ExcelIgnoreUnannotated
@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.LEFT, verticalAlignment = VerticalAlignmentEnum.CENTER)
public class OrderDTO {

    @ExcelProperty(value = "订单编号", order = 1)
    @ExcelMergeColumn
    private String colA;

    @ExcelProperty(value = "创建时间", order = 2)
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @ExcelMergeColumn
    private LocalDateTime colB;

    @ExcelProperty(value = "经销商", order = 3)
    @ExcelMergeColumn
    private String colC;


    @ExcelProperty(value = "明细-ERP编码", order = 4)
    @ExcelMergeColumn
    private String colD;

    @ExcelProperty(value = "明细-商品名称", order = 5)
    @ExcelMergeColumn
    private String colE;

    @ExcelProperty(value = "明细-商品编码", order = 6)
    @ExcelMergeColumn
    private String colF;

    @ExcelProperty(value = "明细-商品规格", order = 7)
    @ExcelMergeColumn
    private String colG;


    @ExcelProperty(value = "明细-规格名称", order = 8)
    @ExcelMergeColumn
    private String colH;


    @ExcelProperty(value = "明细-订货数量", order = 9)
    @ExcelMergeColumn
    private String colI;


    @ExcelProperty(value = "明细-含税单价", order = 10)
    @ExcelMergeColumn
    private String colJ;


    @ExcelProperty(value = "明细-DJ", order = 11)
    @ExcelMergeColumn
    private String colK;


    @ExcelProperty(value = "明细-含税单价合计", order = 12)
    @ExcelMergeColumn
    private String colL;

    @ExcelProperty(value = "订单价税总额", order = 13)
    @ExcelMergeColumn
    private String colM;

    @ExcelProperty(value = "出库信息-出库数量", order = 14)
    @ExcelMergeColumn
    private String colN;

    @ExcelProperty(value = "出库信息-生产日期", order = 15)
    @ExcelMergeColumn
    private String colO;

    @ExcelProperty(value = "出库信息-过期日期", order = 16)
    @ExcelMergeColumn
    private String colP;

    @ExcelProperty(value = "出库信息-批号", order = 17)
    @ExcelMergeColumn
    private String colQ;

    @ExcelProperty(value = "备注", order = 18)
    @ExcelMergeColumn
    private String colR;

    @ExcelProperty(value = "物流公司", order = 19)
    @ExcelMergeColumn
    private String colS;

    @ExcelProperty(value = "物流单号", order = 20)
    @ExcelMergeColumn
    private String colT;

    @ExcelProperty(value = "签收时间", order = 21)
    @ExcelMergeColumn
    private String colU;

    @ExcelProperty(value = "收货人", order = 22)
    @ExcelMergeColumn
    private String colV;

    @ExcelProperty(value = "收货人-手机号", order = 23)
    @ExcelMergeColumn
    private String colW;

    @ExcelProperty(value = "收货信息/省", order = 24)
    @ExcelMergeColumn
    private String colX;
    @ExcelProperty(value = "收货信息/市", order = 25)
    @ExcelMergeColumn
    private String colY;
    @ExcelProperty(value = "收货信息/区（县）", order = 26)
    @ExcelMergeColumn
    private String colZ;
    @ExcelProperty(value = "详细地址", order = 27)
    @ExcelMergeColumn
    private String colAA;


    @ExcelProperty(value = "收票人", order = 28)
    @ExcelMergeColumn
    private String colAB;

    @ExcelProperty(value = "收票人-手机号", order = 29)
    @ExcelMergeColumn
    private String colAC;

    @ExcelProperty(value = "开票信息/省", order = 30)
    @ExcelMergeColumn
    private String colAD;

    @ExcelProperty(value = "开票信息/市", order = 31)
    @ExcelMergeColumn
    private String colAE;
    @ExcelProperty(value = "开票信息/区（县）", order = 32)
    @ExcelMergeColumn
    private String colAF;
    @ExcelProperty(value = "收票地址", order = 33)
    @ExcelMergeColumn
    private String colAG;
    @ExcelProperty(value = "下单组织", order = 34)
    @ExcelMergeColumn
    private String colAH;
    @ExcelProperty(value = "销售组织", order = 35)
    @ExcelMergeColumn
    private String colAI;
    @ExcelProperty(value = "订单状态", order = 36)
    @ExcelMergeColumn
    private String colAJ;
    @ExcelProperty(value = "快递结算", order = 37)
    @ExcelMergeColumn
    private String colAK;
    @ExcelProperty(value = "同步状态", order = 38)
    @ExcelMergeColumn
    private String colAL;
    @ExcelProperty(value = "修改人", order = 39)
    @ExcelMergeColumn
    private String colAM;
    @ExcelProperty(value = "修改时间", order = 40)
    @ExcelMergeColumn
    private String colAN;

    @ExcelProperty(value = "审核人", order = 41)
    @ExcelMergeColumn
    private String colAO;
    @ExcelProperty(value = "审核时间", order = 42)
    @ExcelMergeColumn
    private String colAP;
    @ExcelProperty(value = "数据状态", order = 43)
    @ExcelMergeColumn
    private String colAQ;

    @ExcelIgnore
    private Integer orderStatus;

    @ExcelIgnore
    private String colAopq;

    public void commonMethod() {
        colAJ = IBaseEnum.getLabelByValue(orderStatus, OrderStatusEnum.class);
        colAL = "已同步";
        if (StringUtils.isNotBlank(colAopq)) {
            List<Map> list;
            JSONArray jsonArray = JSONUtil.parseArray(colAopq);
            list = jsonArray.toList(Map.class);
            if (list.size() > 0) {
                for (Map map : list) {
                    if (map.containsKey("operation") && map.get("operation").equals("已审核")) {
                        colAO = map.get("name").toString();
                        if (map.containsKey("time")) {
                            Long time = Long.valueOf(map.get("time").toString());
                            colAP = DataConstatns.dateFormat.format(new Date(time));
                        }
                        colAQ = map.get("operation").toString();
                    }
                }
            }
        }
    }
}
